Skip to content

Add support for pytest#169

Closed
different-name wants to merge 1 commit intopython:mainfrom
different-name:fix-test-imports
Closed

Add support for pytest#169
different-name wants to merge 1 commit intopython:mainfrom
different-name:fix-test-imports

Conversation

@different-name
Copy link

Fix for the following test error:

============================= test session starts ==============================
platform linux -- Python 3.13.12, pytest-9.0.2, pluggy-1.6.0
rootdir: /build/srctools-2.6.2
configfile: pyproject.toml
plugins: regressions-2.9.1, datadir-1.8.0
collecting ... ^Mcollecting 1585 items / 1 error                                                ^Mcollected 3619 items / 1 error                                                 

==================================== ERRORS ====================================
____ ERROR collecting src/pythoncapi-compat/tests/test_pythoncapi_compat.py ____
ImportError while importing test module '/build/srctools-2.6.2/src/pythoncapi-compat/tests/test_pythoncapi_compat.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/nix/store/m1fw8l8y9ycxh5dzispbb7cwl6rra14l-python3-3.13.12/lib/python3.13/importlib/__init__.py:88: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
src/pythoncapi-compat/tests/test_pythoncapi_compat.py:24: in <module>
    from utils import run_command, command_stdout
E   ModuleNotFoundError: No module named 'utils'
=========================== short test summary info ============================
ERROR src/pythoncapi-compat/tests/test_pythoncapi_compat.py
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
=============================== 1 error in 0.82s ===============================

Not sure why this is only an issue now, however this seems to be the correct approach

@vstinner
Copy link
Member

vstinner commented Mar 2, 2026

It seems like you ran pytest command to run tests. It's not supported.

You should run ./runtests.py command instead. See the documentation: https://pythoncapi-compat.readthedocs.io/en/latest/tests.html

@different-name different-name changed the title Use relative import for utils in test_pythoncapi_compat Add support for pytest Mar 3, 2026
@different-name
Copy link
Author

What is the reason for using the custom script?

@vstinner
Copy link
Member

vstinner commented Mar 3, 2026

What is the reason for using the custom script?

runtests.py tries to test as many available Python versions as possible by testing these programs:

PYTHONS = (
    # CPython
    "python3-debug",
    "python3",
    "python2.7",
    "python3.6",
    "python3.7",
    "python3.8",
    "python3.9",
    "python3.10",
    "python3.11",
    "python3.12",
    "python3.13",
    "python3.13t",
    "python3.14",
    "python3.14t",
    "python3.15",
    "python3.15t",

    # PyPy
    "pypy",
    "pypy2",
    "pypy2.7",
    "pypy3",
    "pypy3.6",
    "pypy3.7",
    "pypy3.8",
    "pypy3.9",
    "pypy3.10",
    "pypy3.11",
)

pytest only runs tests on a single Python version.

@different-name
Copy link
Author

Is there a tradeoff to using the relative import here?

For context, I maintain the srctools package in nixpkgs & testing for python packages is handled by pytest. In this case, the test is run for each build of the package (currently Python 3.13 & 3.14), as those are the versions of Python available in nixpkgs

We would have to use the --current flag to use runtests.py, as those other versions of Python are not available in nixpkgs. Does using your runtests.py with --current provide any benefit over pytest?

@erlend-aasland
Copy link

I don't see the need for this change. As Victor points out, the docs show how to run the tests. Suggesting to close this.

@different-name
Copy link
Author

I don't see the need for this change

Could you please explain what the drawback to using a relative import here is? I don't understand

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants